package com.jzlx;

import com.jzlx.threads.DaemonAwaitThread;
import com.jzlx.websocket.WebSocketClient;

import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
import java.io.IOException;
import java.net.URI;

public class Main {
    public static void main(String[] args) {

        //设置非守护线程
        DaemonAwaitThread daemonAwaitThread = new DaemonAwaitThread();
        daemonAwaitThread.setDaemon(false);
        daemonAwaitThread.start();
        // WebSocket服务器地址
        String serverUri = null;
        for (String arg :
                args) {
            serverUri = arg;
        }
        // WebSocket服务器地址
        if (serverUri == null)
            serverUri = "ws://127.0.0.1:12001/xian-resource-manage-service/websocket/monitor";
        System.out.println("server url: " + serverUri);
        //维护状态重连接线程
        String finalServerUri = serverUri;
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    if (WebSocketClient.getOnlineCount() == 0) {
                        // 创建WebSocket容器
                        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
                        // 连接到服务器
                        Session session = null;
                        try {
                            session = container.connectToServer(WebSocketClient.class, URI.create(finalServerUri));
                            session.getBasicRemote().sendText("ping");
//                            MassageInfo info = new MassageInfo(OSHIUtil.getOperatingSystemInfo(), OSHIUtil.getCpuInfo(), OSHIUtil.getMemoryInfo(MemoryInfo.MB), OSHIUtil.getDiskSpaceInfo(DiskSpaceInfo.GB));
//                            session.getBasicRemote().sendText(JSON.toJSONString(info));
                        } catch (DeploymentException | IOException e) {
                            e.printStackTrace();
                        }
                    }
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread.start();
        System.out.println("程序启动完成");
    }
}